<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('请选择优惠券','error');
	}	
	$log=pdo_get('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$coupon=$this->model('coupon')->intcoupon($log['couponid'],$log['createtime']);
	$coupon['isuse']=$log['isuse'];
	$coupon['ordersn']=$log['ordersn'];
	$coupon['limitgoodids']= explode(';', $coupon['limitgoodids']);
	$coupon['limitgoodids']= substr(implode(',', $coupon['limitgoodids']),1,strlen(implode(',', $coupon['limitgoodids']))-2);
	$coupon['limitgoodcateids']= explode(';', $coupon['limitgoodcateids']);
	$coupon['limitgoodcateids']= substr(implode(',', $coupon['limitgoodcateids']),1,strlen(implode(',', $coupon['limitgoodcateids']))-2);
	if ($coupon['limitgoodtype'] != 0) {
		if (!empty($coupon['limitgoodids'])) {
			$where = 'and id in(' . $coupon['limitgoodids'] . ')';
		}
		$goods = pdo_fetchall('select `title` from ' . tablename('jy_weishop_goods') . ' where uniacid=:uniacid ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
	}
	if ($coupon['limitgoodcatetype'] != 0) {
		if (!empty($coupon['limitgoodcateids'])) {
			$where = 'and id in(' . $coupon['limitgoodcateids'] . ')';
		}
		$group = pdo_fetchall('select `title`  from ' . tablename('jy_weishop_goods_group') . ' where uniacid=:uniacid   ' . $where, array(':uniacid' => $_SESSION['uniacid']), 'id');
	}
	$return['setting']=$this->plugin_setting('coupon');
	$return['goods']=$goods;
	$return['group']=$group;
	$return['getlogid']=$id;
	$coupon['hexiaourl']=custom_url('jy_weishop/coupon.hexiao//getlogid='.$id,true);
	if($coupon['coupontype'] == '3'){
		$prizeType=plugin('jy_weishop_liping')->prizeType;
		foreach($coupon['reward'] as &$rv){
			$rv['typetitle']=$prizeType[$rv['type']];
			$info='送【'.$rv['typetitle'].'】';
			if($rv['type']==1){
				$goodstitle=pdo_getcolumn('jy_weishop_goods',array('uniacid' => $_SESSION['uniacid'],'id'=>$rv['goodsid']),'title');
				$info.=':'.$goodstitle;
			}elseif($rv['type']==9){
				$coupontitle=pdo_getcolumn('jy_weishop_coupon',array('uniacid' => $_SESSION['uniacid'],'id'=>$rv['couponid']),'title');
				$info.=':'.$coupontitle;
			}elseif($rv['type']==7){
				$info.=':随机'.$rv['redmin'].'~'.$rv['rexmax'];
			}else{
				$info.=':'.$rv['jifen'].'个';
			}
			$rv['info']=$info;
		}
	}
	$return['coupon']=$coupon;
	if($_SESSION['wxapp_id']){
		$url='/jy_weishop/coupon/hexiao/index?getlogid='.$id;
		$pid='couponhexiao'.$id;
		$return['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
	}
	if($coupon['coupontype']==1 && $coupon['isuse']!=1 && $coupon['past']!=1){
		$sharelink='jy_weishop/coupon.songget//getlogid='.$id;
		$return['sharedata']=array(
				'title'=>'好友赠送：'.$coupon['title'].' '.$coupon['title2'].' '.$coupon['title3'],
				'desc'=>'点击领取：'.$coupon['title'].' '.$coupon['title2'].' '.$coupon['title3'],
				'link'=>custom_url($sharelink,true),
				'imgUrl'=>tomedia('../addons/jy_weishop/images/icon_tejia.jpg')
		);
	}
	set_html('title','优惠券详情');
	exi($return);
}
if($_RQ['op']=='getliping'){
	$post=get_postdata();
	$getlogid=intval($_RQ['getlogid']);
	model('jy_weishop/coupon')->usecoupon($getlogid);
	exi('收礼成功！');	
}
if($_RQ['op']=='getgoods'){
	$getlogid=intval($_RQ['getlogid']);
	$getlog=pdo_get('jy_weishop_coupon_getlog',array('id'=>$getlogid,'uniacid'=>$_SESSION['uniacid']),array('mid','couponid'));
	$coupon_detailid=$getlog['couponid'];
	$coupon_detail=pdo_get('jy_weishop_coupon',array('id'=>$coupon_detailid,'uniacid'=>$_SESSION['uniacid']));
	if($coupon_detailid && $coupon_detail['coupontype']=='3'){
		$reward=json_decode($coupon_detail['reward'],true);
		foreach($reward as $rv){
			if($rv['type']==1){
				$isgoods=1;
			}
		}
	}
	//商品直接跳转到下单页面：
	if($isgoods){
		exi('','','jy_weishop/order.create//couponlogid='.$getlogid);
	}else{
		exi('不存在商品需要领取！','error');
	}
}
?>